package com.zhuiYun.project.api.userAddress.controller;

import com.zhuiYun.project.api.userAddress.entiy.UserAddress;
import com.zhuiYun.project.api.userAddress.model.UserAddressModel;
import com.zhuiYun.project.api.userAddress.service.UserAddressService;
import com.zhuiYun.project.common.controller.BaseController;
import com.zhuiYun.project.common.errorCode.CommonException;
import com.zhuiYun.project.common.response.CommonResult;
import com.zhuiYun.project.util.pageUtil.PageRequest;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @ClassName UserAddressController
 * @Description 收货地址管理
 * @Author 胡亮
 * @Date 2023/04/04 上午 10:14
 **/
@RestController
@Api(tags = "收货地址管理")
@RequestMapping("/userAddress")
@Slf4j
@CrossOrigin
public class UserAddressController extends BaseController {
    @Autowired
    UserAddressService userAddressService;

    /**
    * @Description 根据用户id获取收货地址
    * @Author lzy
    * @Date 2023/4/6 19:22
    * @Return com.zhuiYun.project.common.response.CommonResult
    * @param userId
    */
    @GetMapping("/getAddressByUserId")
    @ApiOperation(value = "根据用户id获取收货地址")
    public CommonResult getAddressByUserId(Integer userId){
        return userAddressService.getAddressByUserId(userId);
    }

    /**
    * @Author 胡亮
    * @Description 收货地址管理的新增方法
    * @Date 2023/04/04 下午 08:28
    * @Return [com.zhuiYun.project.api.userAddress.model.UserAddressModel]
    */
    @PostMapping("/saveAddress")
    @ApiOperation(value = "收货地址的新增方法")
    public CommonResult saveAddress(@RequestBody UserAddressModel userAddressModel) throws CommonException {
        log.info("新增收货地址");
        return userAddressService.saveAddress(userAddressModel);
    }

    /**
    * @Author 胡亮
    * @Description 分页查询
    * @Date 2023/04/05 上午 08:31
    * @Return [com.zhuiYun.project.util.pageUtil.PageRequest]
    */
    @GetMapping("/getAddressByPage")
    @ApiOperation(value = "收货地址的分页查询方法")
    public CommonResult getAddressByPage(PageRequest pageRequest, Integer id) throws CommonException {
        log.info("分页查询");
        return userAddressService.getUserByPage(pageRequest,id);
    }

    /**
    * @Author 胡亮
    * @Description  收货地址的删除方法
    * @Date 2023/04/05 上午 10:33
    * @Return [java.lang.Integer]
    */
    @DeleteMapping("/delAddressById/{id}")
    @ApiOperation(value = "收货地址的删除方法")
    public CommonResult delAddressById(@PathVariable Integer id) throws CommonException {
        log.info("删除收货地址");
        return userAddressService.delAddressById(id);
    }

    /**
    * @Author 胡亮
    * @Description 修改收货的修改方法
    * @Date 2023/04/05 上午 10:52
    * @Return [com.zhuiYun.project.api.sysUser.model.SysUserModel]
    */
    @PostMapping("/updateAddress")
    @ApiOperation(value = "修改收货地址的方法")
    public CommonResult updateAddress(@RequestBody UserAddressModel userAddressModel) throws CommonException {
        log.info("修改收货地址");
        return userAddressService.updateAddress(userAddressModel);
    }

    /**
    * @Author 胡亮
    * @Description 修改默认收货地址(默认地址唯一)
    * @Date 2023/04/05 上午 10:51
    * @Return [java.lang.Integer]
    */
    @PostMapping("/updateAddressById")
    @ApiOperation(value = "修改默认收货地址方法")
    public CommonResult updateAddressById(@RequestBody UserAddressModel userAddressModel) throws CommonException {
        log.info("修改默认收货地址(默认地址唯一)");
        return userAddressService.updateAddressById(userAddressModel);
    }

    /**
    * @Author gpd
    * @Description  新增用户地址（uniapp新增用户地址）
    * @Date 2023-04-15 上午 11:05
    * @Return com.zhuiYun.project.common.response.CommonResult

    */
    @PostMapping("/addUserAddress")
    @ApiOperation(value = "uniapp新增用户收货地址方法")
    public CommonResult addUserAddress (@RequestBody UserAddress userAddress){
        return CommonResult.ok(userAddressService.addUserAddress(userAddress));
    }

    /**
    * @Author gpd
    * @Description 查询用户的所有收货地址 （uniapp地址管理）
    * @Date 2023-04-15 上午 11:45
    * @Return com.zhuiYun.project.common.response.CommonResult
    * @param userId
    */
    @GetMapping("/getUserAllAddress")
    @ApiOperation(value = "uniapp查询用户的所有收货地址")
    public CommonResult getUserAllAddress(Integer userId){
        return CommonResult.ok(userAddressService.getUserAllAddress(userId));
    }

    /**
    * @Author gpd
    * @Description 查询用户点击的地址详情 （uniapp 地址修改）
    * @Date 2023-04-15 下午 12:15
    * @Return com.zhuiYun.project.common.response.CommonResult
    * @param id
    */
    @GetMapping("/getUserSelectAddress")
    @ApiOperation(value = "uniapp查询用户点击的地址详情")
    public CommonResult getUserSelectAddress(Integer id){
        return CommonResult.ok(userAddressService.getUserSelectAddress(id));
    }

    /**
    * @Author gpd
    * @Description 修改用户地址 （uniapp 修改地址）
    * @Date 2023-04-15 下午 12:27
    * @Return com.zhuiYun.project.common.response.CommonResult

    */
    @PostMapping("/updateUserAddress")
    @ApiOperation(value = "uniapp修改用户地址")
    public CommonResult updateUserAddress(@RequestBody UserAddress userAddress ){
        return CommonResult.ok(userAddressService.updateUserAddress(userAddress));
    }
    /**
     * @Author lyp
     * @Description 修改默认收货地址(默认地址唯一)
     * @Date 2023/04/15 14:09
     * @Return CommonResult
     */
    @PostMapping("/updateAddressByIdApplet")
    @ApiOperation(value = "修改默认收货地址方法")
    public CommonResult updateAddressByIdApplet(@RequestBody UserAddressModel userAddressModel) throws CommonException{
         log.info("修改默认收货地址(默认地址唯一)");
         return userAddressService.updateAddressById(userAddressModel);
    }

    /**
     * @Author lyp
     * @Description  收货地址的删除方法
     * @Date 2023/04/15 14:10
     * @Return CommonResult
     */
    @DeleteMapping("/delAddressByIdApplet")
    @ApiOperation(value = "收货地址的删除方法")
    public CommonResult delAddressByIdApplet(@RequestBody Integer id) throws CommonException {
        log.info("删除收货地址");
        return userAddressService.delAddressById(id);
    }
}
